﻿using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;

namespace TaoNiuOA.Models
{
    /// <summary>
    /// 任务实体
    /// </summary>
    public class Task
    {
        [Key]
        public int TaskID { get; set; }

        [Required]
        [Display(Name = "任务名称")]
        public string Name { get; set; }

        [Required]
        [Display(Name = "任务类型")]
        public int TaskTypeID { get; set; }

        [Display(Name = "发布时间")]
        public DateTime PubTime { get; set; }

        [Required]
        [Display(Name = "任务内容")]
        [StringLength(900000000, ErrorMessage = "你输入的内容过长!")]
        public string Content { get; set; }

        [Display(Name = "任务绩效")]
        public double Point { get; set; }

        [Display(Name = "任务期限")]
        public DateTime ProjectLast { get; set; }

        [Display(Name = "任务所需天数")]
        public double TotalDays { get; set; }

        [Display(Name = "任务附件")]
        public string Attachment { get; set; }

        //0为低,1为普通,2为高,3为紧急
        [Display(Name = "任务级别")]
        public int Level { get; set; }

        [Display(Name = "完成进度")]
        public string Process { get; set; }

        [Display(Name = "开始时间")]
        public DateTime? StartTime { get; set; }

        [Display(Name = "结束时间")]
        public DateTime? EndTime { get; set; }

        [Display(Name = "已持续天数")]
        public double LastDays { get; set; }

        /// <summary>
        /// 0为未被领取,1已被领取,2为执行中,3已完成
        /// </summary>
        [Display(Name = "任务状态")]
        public int Status { get; set; }

        [Display(Name = "最后更新")]
        public DateTime? LastUpdate { get; set; }

        [Display(Name = "负责人")]
        public int? UserID { get; set; }

        [Display(Name = "参与者")]//多个用英文逗号,分开
        public string Partake { get; set; }

        /// <summary>
        /// 审批人
        /// </summary>
        [Display(Name="审批人")]
        public int? ApproveID { get; set; }

        [Display(Name = "是否合格")]//0为不合格,1为合格
        public bool? IsGood { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [Display(Name="备注")]
        public string Remark { get; set; }

        TaoNiuOA.Models.DbEntity db = new TaoNiuOA.Models.DbEntity();
        /// <summary>
        /// 获取任务类型
        /// </summary>
        /// <returns></returns>
        public TaskType GetTaskType()
        {
            var tt = db.TaskTypes.SingleOrDefault(n => n.TaskTypeID == this.TaskTypeID);
            if (tt == null)
            {
                tt = new TaskType();
                tt.TaskTypeID = 0;
                tt.TypeName = "未定义";
            }
            return tt;
        }

        /// <summary>
        /// 该任务的负责人信息
        /// </summary>
        /// <returns></returns>
        public User GetUser()
        {
            var u = db.Users.SingleOrDefault(n => n.UserID == this.UserID);
            if (u == null)
            {
                u = new User();
                u.UserID = 0;
                u.UserName = "未定义";
            }
            return u;
        }

        /// <summary>
        /// 该任务的审批人
        /// </summary>
        /// <returns></returns>
        public User GetApprover()
        {
            var u = db.Users.SingleOrDefault(n => n.UserID == this.ApproveID);
            if (u == null)
            {
                u = new User();
                u.UserID = 0;
                u.UserName = "未定义";
            }
            return u;
        }
    }
}